package com.changgou.business.listener;

import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: zhengke
 * @date: 2020/12/17
 * @return:
 * @description: 大广告发送预热请求更新的Mq监听器
 */

@Component
public class AdUpdateListener {

    @RabbitListener(queues = "ad_update_queue") //queue名字
    public void adUpdate(String position){

        //拼接预热请求的URL
        String url="http://192.168.200.128/ad_update?position="+position;

        //利用OkHttpClient来创建请求示例
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request = new Request.Builder().url(url).build();

        //执行发送请求  回调 异步的
        Call call = okHttpClient.newCall(request);

        //对得到请求之后的结果进行处理
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("发送预热请求失败："+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("发送预热请求成功");
            }
        });
    }
}
